package com.go.ssh.point.dao.hibernate;

import java.util.List;

import org.springframework.util.CollectionUtils;

import com.go.ssh.commons.Constants;
import com.go.ssh.commons.dao.hibernate.BaseHibernateDao;
import com.go.ssh.point.dao.IGoodsCodeDao;
import com.go.ssh.point.model.GoodsCodeModel;

public class GoodsCodeHibernateDao extends
		BaseHibernateDao<GoodsCodeModel, Integer> implements IGoodsCodeDao {

	@Override
	// 根据商品ID查询该商品的兑换码
	public List<GoodsCodeModel> listAllByGoods(int pn, int goodsId) {
		final String hql = getListAllHql() + " where goods.id = ?";
		return list(hql, pn, Constants.DEFAULT_PAGE_SIZE, goodsId);
	}

	@Override
	// 根据商品ID统计该商品的兑换码数量
	public int countAllByGoods(int goodsId) {
		final String hql = getCountAllHql() + " where goods.id = ?";
		Number result = unique(hql, goodsId);
		return result.intValue();
	}

	@Override
	// 获取一个指定商品的没有被兑换的兑换码
	public GoodsCodeModel getOneNotExchanged(int goodsId) {
		String hql = getListAllHql()
				+ " where goods.id = ? and exchanged=false";
		List<GoodsCodeModel> result = list1(hql, goodsId);
		if (CollectionUtils.isEmpty(result)) {
			return null;
		}
		return result.get(0);
	}

}
